<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>用户</title>
    <link rel="stylesheet" href="layui/css/layui.css">
</head>
<body>
<div class="layui-container" style="padding-top: 30px;">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>用户列表</legend>
    </fieldset>
    <table class="layui-hide" id="test" style="width: 300px" lay-filter="test"></table>
    <script type="text/html" id="barDemo">
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </script>
    <script src="layui/layui.js" charset="utf-8"></script>
    <script>
        layui.use(['table','jquery'], function(){
            var table = layui.table;
            var $ = layui.jquery;
            table.render({
                elem: '#test'
                ,url:'/userApi/userList'
                ,title: '用户列表'
                ,cols: [
                    [
                        {field:'id', title: '编号', sort: true}
                        ,{field:'username', title: '用户名'}
                        ,{field:'nickname', title: '昵称'}
                        ,{field:'gender', title: '性别'}
                        ,{field:'telephone', title: '联系电话'}
                        ,{field:'registerdate', title: '注册日期'}
                        ,{field:'address', title: '地址'}
                        ,{fixed: 'right', title:'操作', toolbar: '#barDemo', align:'center', width:80}
                    ]
                ]
                ,page: true
            });

            //监听行工具事件
            table.on('tool(test)', function(obj){
                var data = obj.data;
                if(obj.event === 'del'){
                    layer.confirm('真的删除【'+data.nickname+'】么', function(index){
                        obj.del(); //删除对应行（tr）的DOM结构
                        layer.close();
                        //向服务端发送删除指令
                        $.ajax({
                            type:'DELETE',
                            url:'/userApi/deleteUserById/'+data.id,
                            success:function (res) {
                                layer.msg(res.data);
                            }
                        })
                    });
                }
            });
        });
    </script>

</div>
<script>
    //二级菜单联动
    layui.use('element', function(){
        var element = layui.element;

    });
</script>
</body>
</html>